443B - Kolya and Tandem Repeat - CodeForces Solution


brute force implementation strings *1500

Please click on ads to support us..

Python Code:

s = input() + 'X' * int(input())
for v in range(len(s) - len(s) % 2, 0, -2):
    for i in range(len(s) - v + 1):
        if all(s[j + v // 2] in ('X', s[j]) for j in range(i, i + v // 2)):
            print(v), exit()

C++ Code:

#include <array>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <sstream>

typedef long long ll;

using namespace std;

void solve() {
    string s;
    int k;

    cin >> s >> k;
    int n = s.size();

    if(k >= n){
        cout << (n+k)/2*2 << endl;
        return;
    }

    int ret = 2 * k;

    for(int i = n-k-1; i >= 0; i--){
        int max_h = (n + k - i) / 2;
        for(int h = 1; h <= max_h; h++){
            bool found = true;
            for(int j = 0; j < h; j++){
                if(i+j+h >= n) break;
                if(s[i+j] != s[i+j+h]){
                    found = false;
                    break;
                }
            }
            if(found){
                ret = max(ret, 2 * h);
            }
        }
    }
    cout << ret << endl;
    return;
}

int main(){
    // ios_base::sync_with_stdio(false);
    // cin.tie(NULL);
    int t = 1;
    // cin >> t;
    while(t--)
        solve();

    return 0;
}


Comments

Submit
0 Comments
More Questions

822B - Crossword solving
1623A - Robot Cleaner
884B - Japanese Crosswords Strike Back
862B - Mahmoud and Ehab and the bipartiteness
429A - Xor-tree
1675C - Detective Task
950A - Left-handers Right-handers and Ambidexters
672B - Different is Good
1C - Ancient Berland Circus
721A - One-dimensional Japanese Crossword
1715B - Beautiful Array
60B - Serial Time
453A - Little Pony and Expected Maximum
1715A - Crossmarket
1715C - Monoblock
1512C - A-B Palindrome
1679B - Stone Age Problem
402A - Nuts
792A - New Bus Route
221A - Little Elephant and Function
492C - Vanya and Exams
1369B - AccurateLee
892B - Wrath
999A - Mishka and Contest
727C - Guess the Array
1625C - Road Optimization
1715D - 2+ doors
267A - Subtractions
1582A - Luntik and Concerts
560A - Currency System in Geraldion